var app = getApp()
Page({

  data: {
    currentIndex: 0,
    status: "all",
    page: 0,
    isShowModal:false,
    moreDataTip: "正在加载中...",
    tkOrderid: "",
    tkMoney: "",
    tkReason: "",
    tkContact: ""
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.title){
      that.setData({
        title:e.title
      })
    }
    app.setNavtitle(that.data.title ? that.data.title:'订单');
      
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.loadData();
      }
    } else {
      that.loadData();
    }
  },
  itemClick: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var statu = "";
    if (id == 0) {
      statu = "all"
    } else if (id == 1) {
      statu = "nopay"
    } else if (id == 2) {
      statu = "hadpay"
    } else if (id == 3) {
      statu = "finish"
    }

    that.setData({
      currentIndex: id,
      status: statu,
      datasource: []
    })
    that.loadData();

  },
  onPullDownRefresh: function () {
    var that = this;
    that.loadData();

  },
  onReachBottom: function () {
    var that = this;
    that.loadMoreData();
  },
  loadData: function () {
    var that = this;
    that.setData({
      datasource: null,
      moreDataTip: "正在加载中...",
    })
    if (!that.datasource) {
      wx.showLoading({
        title: '加载中',
      })
    }
    console.log(that.data.status)
    wx.request({
      url: app.globalData.requestUrl,

      // url: app.getRequestUrl("/applet.php?map=applet_appointment_order_list"),
      data: {
        map:'applet_appointment_order_list',
        status: that.data.status,
        page: 0,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {
        console.log(res)
        if (res.data.ec == 200) {
          that.setData({
            datasource: res.data.data,
            page: 0
          })
        } else {
          that.setData({
            moreDataTip: "没有更多数据了",
            datasource: []
          })
        }
      },
      fail: function () {
      },
      complete: function () {
        wx.stopPullDownRefresh();
wx.hideLoading()
      }
    })
  },
  loadMoreData: function () {
    var that = this;
    var currenpage = that.data.page + 1

    console.log(that.data.status)
    wx.request({
      url: app.globalData.requestUrl,

      // url: app.getRequestUrl("/applet.php?map=applet_appointment_order_list"),
      data: {
        map: 'applet_appointment_order_list',

        status: that.data.status,
        page: currenpage,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {
        console.log(res)
        if (res.data.ec == 200) {
          var caurunData = that.data.datasource;

          caurunData = caurunData.concat(res.data.data)
          console.log(caurunData.length)
          that.setData({
            datasource: caurunData,
            page: currenpage
          })
        } else {
          that.setData({
            moreDataTip: "没有更多数据了"
          })
        }
      },
      fail: function () {

      }
    })
  },
  getcurrentPage: function () {
    var that = this;

    var currentPage = 0;
    switch (that.data.currentIndex) {
      case 0:
        currentPage = that.data.allpage

        break;
      case 1:
        currentPage = that.data.nopaypage
        break;
      case 2:
        currentPage = that.data.hadpaypage

        break;
      case 3:
        currentPage = that.data.finishpage

        break;

    }

    return currentPage;
  },
  setcurrentPage: function () {
    var that = this;
    var addpage = that.getcurrentPage() + 1;

    switch (that.data.currentIndex) {
      case 0:
        that.setData({
          allpage: addpage
        })
        break;
      case 1:
        that.setData({
          nopaypage: addpage
        })
        break;
      case 2:
        that.setData({
          hadpaypage: addpage
        })
        break;
      case 3:
        that.setData({
          finishpage: addpage
        })
        break;

    }

  },
  applyTuikuan: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.tid;
    var total = e.currentTarget.dataset.total;
    that.setData({
      tkMoney: total,
      tkReason: "",
      tkContact: "",
      isShowModal: true,
      tkOrderid: orderId
    })
  },
  hideModal: function () {
    this.setData({
      isShowModal: false
    })
  },
  tkMoneyChange: function (e) {
    this.setData({
      tkMoney: e.detail.value
    })
  },
  tkReasonChange: function (e) {
    this.setData({
      tkReason: e.detail.value
    })
  },
  tkContactChange: function (e) {
    this.setData({
      tkContact: e.detail.value
    })
  },
  submitTkapply: function () {
    var that = this;
    app.getSubId(that, 'applet_order_refund', 'toSubmitTkapply');
  },
  toSubmitTkapply: function () {
    var that = this;
    var data = {};
    data.map = 'applet_order_refund';
    // data.suid = app.globalData.suid,
    data.tid = that.data.tkOrderid;
    data.money = that.data.tkMoney;
    data.reason = that.data.tkReason;
    data.contact = that.data.tkContact;

    if (data.money == "") {
      app.errorTip(that, "请输入退款金额", 2000);
      return;
    }
    // if (parseFloat(data.money) > that.data.orderDetail.total) {
    //   app.errorTip(that, "退款金额不能大于商品总金额", 2000);
    //   return;
    // }
    if (data.reason == "") {
      app.errorTip(that, "请输入退款原因", 2000);
      return;
    }
    if (data.contact == "") {
      app.errorTip(that, "请输入您的联系方式", 2000);
      return;
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          app.errorTip(that, res.data.data.msg, 2000);
          setTimeout(function(){
            that.onLoad();
          },1000);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        that.setData({
          isShowModal: false
        })
        wx.hideToast();
      }
    });
  },
  cancelTk: function (e) {
    var that = this;
    var tid = e.target.dataset.tid;
    wx.showModal({
      title: '',
      cancelText: '再考虑下',
      confirmText: '确认',
      content: '确认撤销维权吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showToast({
            title: '加载中',
            icon: 'loading',
            mask: true,
            duration: 10000
          });
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_refund_cancel',
              // suid: app.globalData.suid,
              tid: tid
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                setTimeout(function () {
                  that.onLoad();
                }, 1000);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.em,
                  showCancel: false
                });
              }
            },
            complete: function () {
              wx.hideToast();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  seeWeiquan: function (e) {
    var that = this;
    var orderId = e.target.dataset.tid;
    console.log(orderId);
    wx.navigateTo({
      url: '/subpages/feedbackRecord/feedbackRecord?orderid=' + orderId
    })
  },
  //取消订单
  cancelOrder: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.tid;
    // console.log(index)
    // console.log(tid);
    wx.request({
      url: app.globalData.requestUrl,

      // url: app.getRequestUrl("/applet.php?map=applet_cancel_order"),
      data: {
        map:'applet_cancel_order',
        tid: id,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {

        console.log(res)
        if (res.data.ec == 200) {
          that.loadData()
          that.showTost(res.data.data.msg)
        } else {
          that.showTost(res.data.em)

          // wx.showToast({
          //   title: res.data.em,
          // })
        }
      },
      fail: function () {

      }
    })
  },
  //付款点击
  paymentClick: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.tid;
    wx.request({
      url: app.globalData.requestUrl,

      // url: app.getRequestUrl("/applet.php?map=applet_order_pay"),
      data: {
        map:'applet_order_pay',
        tid: id,
        money: that.data.datasource[index].total,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              console.log(res);
              console.log("支付成功");
              that.showTost("支付成功")
              that.loadData();
              // that.setData({
              //   moneyVal: "",
              //   isDisable: true
              // })
              // wx.navigateTo({
              //   url: '/pages/successtip/successtip?money=' + data.money
              // })
            },
            'fail': function (res) {
              that.showTost("支付失败")
              // wx.showModal({
              //   title: '',
              //   content: '支付失败',
              //   showCancel: false
              // });
              // wx.navigateTo({
              //   url: '/pages/successtip/successtip?money=' + data.money
              // })
            }
          });
        } else {
          wx.hideLoading()
          that.showTost(res.data.em);


        }
      },
      fail: function () {
        wx.hideLoading()

      }
    })
  },
  showTost: function (toast) {
    wx.showToast({
      title: toast,
      image: "/images/hud_info.png",
      // icon: "loading",
      duration: 1000,
      mask: true
    })
  },
  openDetail:function(e){
    var taht = this;
    var tid = e.currentTarget.dataset.tid;
    if(tid){
      wx.navigateTo({
        url: '/subpages/GeneralreservationOrderDetail/GeneralreservationOrderDetail?tid='+tid,
      })
    }
  },

  // setdatasource:function(source){
  //   var that = this;
  //   switch (that.data.currentIndex) {
  //     case 0:
  //       var currentsource = that.data.allsource
  //       currentsource.concat(source)
  //       that.setData({
  //         allsource: currentsource
  //       })
  //       break;
  //     case 1:
  //       var currentsource = that.data.nopaysource
  //       currentsource.concat(source)
  //       that.setData({
  //         nopaysource: currentsource
  //       })
  //       break;
  //     case 2:
  //       var currentsource = that.data.hadpaysource
  //       currentsource.concat(source)
  //       that.setData({
  //         hadpaysource: currentsource
  //       })
  //       break;
  //     case 3:
  //       var currentsource = that.data.finishsource
  //       currentsource.concat(source)
  //       that.setData({
  //         finishsource: currentsource
  //       })
  //       break;

  //   }
  // }
})